// ==UserScript==
// @name           Display Kilometers and Miles
// @namespace      http://glenncarr.com/greasemonkey
// @description    Display both kilometers and miles
// @include        http://www.dailymile.com/*
// @author         Glenn Carr
// $LastChangedRevision: 624 $
// $LastChangedDate: 2012-03-07 22:41:21 +0000 (Wed, 07 Mar 2012) $
// ==/UserScript==

var kmElements = document.evaluate( "//span[@class='workout-distance']/span[@class='workout-distance-units'][contains(.,'km')]/..", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
var mileElements = document.evaluate( "//span[@class='workout-distance']/span[@class='workout-distance-units'][contains(.,'mi')]/..", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

// Convert kilometers to miles
for ( i = 0; i < kmElements.snapshotLength; i++ )
{
	var kmElement = kmElements.snapshotItem( i );
	var miles = parseFloat( kmElement.textContent ) * 0.6214;

	var el = document.createElement( 'span' );
	el.setAttribute( 'class', 'workout-distance' );
	el.innerHTML = ' (' + miles.toFixed(2).toString() + ' <span class="workout-distance-units">mi</span>)';
	kmElement.parentNode.insertBefore( el, kmElement.nextSibling );
}

// Convert miles to km
for ( i = 0; i < mileElements.snapshotLength; i++ )
{
	var mileElement = mileElements.snapshotItem( i );
	var kmElements = parseFloat( mileElement.textContent ) * 1.609344;

	var el = document.createElement( 'span' );
	el.setAttribute( 'class', 'workout-distance' );
	el.innerHTML = ' (' + kmElements.toFixed(2).toString() + ' <span class="workout-distance-units">km</span>)';
	mileElement.parentNode.insertBefore( el, mileElement.nextSibling );
}
